import requests
from utils.config import WRITE_IN_LOG, SEND_RUN_START, WRITE_DOMAIN_IN_LOG


def send_run_status(umail, app_name, outside_ip, local_ip, port, image_id, container_id, app_cn_name, app_desc, app_dock, app_language, app_main_code_file_path, docker_id):
    headers = {
        "Content-type": "application/json"
    }
    data = {
        "outside_ip": outside_ip,
        "local_ip": local_ip,
        "umail": umail,
        "app_name": app_name,
        "port": port,
        "image_id": image_id,
        "container_id": container_id,
        "app_cn_name": app_cn_name,
        "app_desc": app_desc,
        "app_dock": app_dock,
        "app_language": app_language,
        "app_main_code_file_path": app_main_code_file_path,
        "docker_id": docker_id
    }
    try:
        r = requests.post(SEND_RUN_START, json=data,
                          headers=headers, verify=False, timeout=10)
        print(r.json())
        return r.json()
    except Exception as e:
        print(e)

    return {}


def send_domain_log(RecordId, rr, ip):
    headers = {
        "Content-type": "application/json"
    }
    data = {
        "domain_record_id": RecordId,
        "domain_type": "A",
        "domain": f"{rr}.aiapi.pro",
        "ip": str(ip)
    }
    try:
        r = requests.post(
            WRITE_DOMAIN_IN_LOG, json=data, headers=headers, verify=False, timeout=10)
        return r.json()
    except Exception as e:
        print(e)

    return {}


def send_log(docker_name, host, docker_ip, container_id, image_id, status, log_info):
    headers = {
        "Content-type": "application/json"
    }
    data = {
        "host": host,
        "docker_ip": docker_ip,
        "docker_name": docker_name,
        "status": status,
        "container_id": container_id,
        "log_info": str(log_info),
        "image_id": image_id
    }
    try:
        r = requests.post(WRITE_IN_LOG, json=data,
                          headers=headers, verify=False, timeout=10)
        return r.json()
    except Exception as e:
        print(e)

    return {}
